<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Timing your code &mdash; SIMULATeQCD 0.7 documentation</title>
      <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
      <link rel="stylesheet" href="../_static/togglebutton.css" type="text/css" />
      <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/togglebutton.js"></script>
        <script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
    <script src="../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="How to make an input parameter file" href="inputParameter.html" />
    <link rel="prev" title="Terminal output &amp; terminating the program" href="terminalIO.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #343131" >
            <a href="../index.html" class="icon icon-home"> SIMULATeQCD
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../01_gettingStarted/gettingStarted.html">Getting started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="contributions.html">Contributions</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="codeStyle.html">Our coding style</a></li>
<li class="toctree-l2"><a class="reference internal" href="git.html">How to do pull requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="codeStructure.html">General structure of the code</a></li>
<li class="toctree-l2"><a class="reference internal" href="templates.html">Template instantiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="terminalIO.html">Terminal output &amp; terminating the program</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Timing your code</a></li>
<li class="toctree-l2"><a class="reference internal" href="inputParameter.html">How to make an input parameter file</a></li>
<li class="toctree-l2"><a class="reference internal" href="memoryAllocation.html">Memory Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="multiGPU.html">Multi-GPU: Distribution of local lattices on the individual GPUs</a></li>
<li class="toctree-l2"><a class="reference internal" href="testing.html">Testing the code</a></li>
<li class="toctree-l2"><a class="reference internal" href="documenting.html">Documenting your code</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../03_applications/applications.html">Applications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../04_codeBase/codeBase.html">Code base</a></li>
<li class="toctree-l1"><a class="reference internal" href="../05_modules/modules.html">Modules</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu"  style="background: #343131" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">SIMULATeQCD</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="contributions.html">Contributions</a> &raquo;</li>
      <li>Timing your code</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/02_contributions/timer.md.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section class="tex2jax_ignore mathjax_ignore" id="timing-your-code">
<h1>Timing your code<a class="headerlink" href="#timing-your-code" title="Permalink to this headline"></a></h1>
<p>SIMULATeQCD provides a build-in timer called <code class="docutils literal notranslate"><span class="pre">StopWatch</span></code> (<code class="docutils literal notranslate"><span class="pre">src/base/stopWatch.h</span></code>).
It is capable of measuring the time duration CPU-only routines as well as the time duration GPU-kernels.</p>
<p>To initialize the timer you simply call</p>
<div class="highlight-C++ notranslate"><div class="highlight"><pre><span></span><span class="n">StopWatch</span><span class="o">&lt;</span><span class="n">device</span><span class="o">&gt;</span><span class="w"> </span><span class="n">timer</span><span class="p">;</span><span class="w"></span>
</pre></div>
</div>
<p>where <code class="docutils literal notranslate"><span class="pre">device</span></code> is boolean template parameter.
If <code class="docutils literal notranslate"><span class="pre">device</span> <span class="pre">=</span> <span class="pre">false</span></code> then only the time duration of CPU routines of the current rank will be considered.
If <code class="docutils literal notranslate"><span class="pre">device</span> <span class="pre">=</span> <span class="pre">true</span></code> then the time duration of GPU-kernel executions will be considered
as well. <strong>Please do not construct the StopWatch before the CommBase!</strong></p>
<section id="measuring-time-duration">
<h2>Measuring time duration<a class="headerlink" href="#measuring-time-duration" title="Permalink to this headline"></a></h2>
<p>Measuring the time duration is quite easy. Here is an example:</p>
<div class="highlight-C++ notranslate"><div class="highlight"><pre><span></span><span class="c1">// Initialize timer which also measure GPU-Kernel execution times:</span>
<span class="n">StopWatch</span><span class="o">&lt;</span><span class="nb">true</span><span class="o">&gt;</span><span class="w"> </span><span class="n">timer</span><span class="p">;</span><span class="w"></span>

<span class="n">timer</span><span class="p">.</span><span class="n">start</span><span class="p">();</span><span class="w"></span>

<span class="c1">// ... do some computation ...</span>

<span class="n">timer</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span><span class="w"></span>

<span class="n">rootLogger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;First time: &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">timer</span><span class="p">);</span><span class="w"></span>

<span class="n">timer</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span><span class="w"></span>
<span class="n">timer</span><span class="p">.</span><span class="n">start</span><span class="p">();</span><span class="w"></span>

<span class="c1">// ... do some computation ...</span>

<span class="n">timer</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span><span class="w"></span>
<span class="n">rootLogger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Second time: &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">timer</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</section>
<section id="combining-different-timers">
<h2>Combining different timers<a class="headerlink" href="#combining-different-timers" title="Permalink to this headline"></a></h2>
<p>It’s also possible to add/multiply/etc. different timers, e.g.:</p>
<div class="highlight-C++ notranslate"><div class="highlight"><pre><span></span><span class="n">StopWatch</span><span class="o">&lt;</span><span class="nb">true</span><span class="o">&gt;</span><span class="w"> </span><span class="n">timer1</span><span class="p">,</span><span class="w"> </span><span class="n">timer2</span><span class="p">,</span><span class="w"> </span><span class="n">timer3</span><span class="p">;</span><span class="w"></span>

<span class="n">timer1</span><span class="p">.</span><span class="n">start</span><span class="p">();</span><span class="w"></span>

<span class="c1">// ... do some computation ...</span>

<span class="n">timer1</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span><span class="w"></span>

<span class="c1">// The streaming operator is overloaded, so you can directly</span>
<span class="c1">// print the time with the correct unit.</span>
<span class="c1">// The call below could print for example &quot;First time: 5min 34s&quot;:</span>
<span class="n">rootLogger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;First time: &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">timer1</span><span class="p">);</span><span class="w"></span>


<span class="n">timer2</span><span class="p">.</span><span class="n">start</span><span class="p">();</span><span class="w"></span>

<span class="c1">// ... do some computation ...</span>

<span class="n">timer2</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span><span class="w"></span>
<span class="n">rootLogger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Second time: &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">timer2</span><span class="p">);</span><span class="w"></span>


<span class="c1">// Combine different timer</span>
<span class="n">timer3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">timer2</span><span class="p">;</span><span class="w"></span>
<span class="n">timer3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer1</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">timer2</span><span class="p">;</span><span class="w"></span>

<span class="n">timer3</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">timer2</span><span class="p">;</span><span class="w"></span>
<span class="n">timer3</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="n">timer2</span><span class="p">;</span><span class="w"></span>

<span class="n">timer3</span><span class="w"> </span><span class="o">/=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"></span>
<span class="n">timer3</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
</pre></div>
</div>
</section>
<section id="getting-time-in-different-units">
<h2>Getting time in different units<a class="headerlink" href="#getting-time-in-different-units" title="Permalink to this headline"></a></h2>
<p>There are some additional convenience functions to obtain the times in different units:</p>
<div class="highlight-C++ notranslate"><div class="highlight"><pre><span></span><span class="c1">// Get time duration in certain units:</span>
<span class="kt">double</span><span class="w"> </span><span class="n">microseconds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">microseconds</span><span class="p">();</span><span class="w"></span>
<span class="kt">double</span><span class="w"> </span><span class="n">milliseconds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">milliseconds</span><span class="p">();</span><span class="w"></span>
<span class="kt">double</span><span class="w"> </span><span class="n">seconds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">seconds</span><span class="p">();</span><span class="w"></span>
<span class="kt">double</span><span class="w"> </span><span class="n">minutes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">minutes</span><span class="p">();</span><span class="w"></span>
<span class="kt">double</span><span class="w"> </span><span class="n">hours</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">hours</span><span class="p">();</span><span class="w"></span>
<span class="kt">double</span><span class="w"> </span><span class="n">days</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">days</span><span class="p">();</span><span class="w"></span>

<span class="c1">// Get time duration in a human friendly unit, e.g &quot;123ms&quot;</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="w"> </span><span class="n">time_str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">autoFormat</span><span class="p">();</span><span class="w"></span>
</pre></div>
</div>
</section>
<section id="compute-flop-s-and-mb-s">
<h2>Compute FLOP/s and MB/s<a class="headerlink" href="#compute-flop-s-and-mb-s" title="Permalink to this headline"></a></h2>
<p>To compute FLOP/s and MB/s one may use these functions:</p>
<div class="highlight-C++ notranslate"><div class="highlight"><pre><span></span><span class="c1">// set how many bytes were processed (for an MB/s output)</span>
<span class="n">timer</span><span class="p">.</span><span class="n">setBytes</span><span class="p">(</span><span class="mi">500</span><span class="p">);</span><span class="w"></span>
<span class="c1">// return MBytes/s (be sure to call setBytes() before)</span>
<span class="kt">double</span><span class="w"> </span><span class="n">mbytess</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">mbs</span><span class="p">();</span><span class="w"></span>

<span class="c1">// set how many FLOPs were calculated</span>
<span class="n">timer</span><span class="p">.</span><span class="n">setFlops</span><span class="p">(</span><span class="mi">500</span><span class="p">);</span><span class="w"></span>
<span class="c1">// return MFLOP/s (be sure to call setFlops() before)</span>
<span class="kt">double</span><span class="w"> </span><span class="n">mflopss</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">timer</span><span class="p">.</span><span class="n">mflps</span><span class="p">();</span><span class="w"></span>
</pre></div>
</div>
</section>
</section>


           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, LatticeQCD.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>